﻿using EAS.Common.ModUtils;
using EAS.Common.Players;
using EAS.Content.Buffs;
using Microsoft.Xna.Framework;
using Terraria;
using Terraria.Audio;
using Terraria.ID;
using Terraria.ModLoader;

namespace EAS.Content.Projectiles
{
    public class StoneGiantHead : ModProjectile
    {
        public Player Owner => Main.player[Projectile.owner];
        public ref float ShootTimer => ref Projectile.ai[0];
        public ref float RotationMovement => ref Projectile.ai[1];
        public AccPlayer moddedOwner => Owner.GetModPlayer<AccPlayer>();
        public override void SetStaticDefaults()
        {
            ProjectileID.Sets.MinionSacrificable[Projectile.type] = true;
            ProjectileID.Sets.MinionTargettingFeature[Projectile.type] = true;
        }

        public override void SetDefaults()
        {
            Projectile.width = Projectile.height = 30;
            Projectile.netImportant = true;
            Projectile.friendly = true;
            Projectile.ignoreWater = true;
            Projectile.minionSlots = 1f;
            Projectile.penetrate = -1;
            Projectile.tileCollide = false;
            Projectile.minion = true;
            Projectile.coldDamage = true;
            Projectile.DamageType = DamageClass.Summon;
        }
        public void CheckMinionExistance()
        {
            Player player = Main.player[Projectile.owner];
            if (!player.active)
            {
                Projectile.active = false;
                return;
            }
            if (!player.HasBuff<StoneGiantBuff>())
            {
                Projectile.active = false;
            }
        }        
        public void ShootBeam()
        {
            SoundEngine.PlaySound(SoundID.Item20, Owner.Center);
            Vector2 velocity = -Projectile.SafeDirectionTo(Owner.Center) * 10f + Projectile.velocity;
            Projectile.NewProjectile(Projectile.GetSource_FromThis(), Projectile.Center, velocity, ModContent.ProjectileType<Beam>(), Projectile.damage, Projectile.knockBack, Projectile.owner);
            Projectile.netUpdate = true;
        }
        public void TargetNPC(NPC target)
        {
            if (target != null)
            {
                if (Projectile.owner == Main.myPlayer)
                {
                    ShootTimer++;
                    if (ShootTimer % 100f == 0f)
                        ShootBeam();
                    ShootTimer = (ShootTimer == 101f) ? 1f : ShootTimer;
                }
            }
            else
                ShootTimer--;

            ShootTimer = MathHelper.Clamp(ShootTimer, 0f, 101f);
            Projectile.netUpdate = true;
        }
        public override void AI()
        {
            NPC potentialTarget = Projectile.Center.MinionHoming(1200f, Owner);

            CheckMinionExistance();             
            TargetNPC(potentialTarget); 

            Lighting.AddLight(Projectile.Center, Color.Fuchsia.ToVector3());
            Projectile.Center = Owner.Center + RotationMovement.ToRotationVector2() * 100f; 
            Projectile.rotation += MathHelper.ToRadians(6.25f * Owner.direction); 
            Projectile.scale = MathHelper.Lerp(1f, 1.005f, ShootTimer % 100f); 
            RotationMovement += MathHelper.ToRadians(1.25f * Owner.direction); 
        }

        public override bool? CanDamage() => false;
    }
}
